package com.example.mysqlite;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.List;

public class MainActivity extends AppCompatActivity{

    private MySQLiteOpenHelper openHelper;
    private EditText etName, etNumber, etGender, etScore;
    private Button  btnInsert,btnDel,btnUpdate,btnQuery;
    private TextView tvStudents;
    private ListView lvStudents;
    private StudentAdapter studentAdapter;
    private List<StudentBean> students;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initView();

        openHelper = new MySQLiteOpenHelper(this);
        students = openHelper.queryAllFromDb();

        studentAdapter = new StudentAdapter(this, students);
        lvStudents.setAdapter(studentAdapter);

        lvStudents.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.d("MainActivity", "position -- " + String.valueOf(position));

                // 获取点击项对应数据ID
                int itemId = (int)studentAdapter.getItemId(position);

                Log.d("MainActivity", "itemId -- " + String.valueOf(itemId));
                List<StudentBean> students = openHelper.queryFromDbById(itemId);

                Log.d("MainActivity", "students.size() -- " + String.valueOf(students.size()));

                if (students.size() == 1){
                    StudentBean student = new StudentBean();
                    student = students.get(0);
                    etName.setText(student.getName());
                    etNumber.setText(student.getNumber());
                    etGender.setText(student.getGender());
                    etScore.setText(student.getScore());
                }
            }
        });

        btnInsert.setOnClickListener(v -> {
            StudentBean studentBean = new StudentBean();
            studentBean.setName(etName.getText().toString().trim());
            studentBean.setNumber(etNumber.getText().toString().trim());
            studentBean.setGender(etGender.getText().toString().trim());
            studentBean.setScore(etScore.getText().toString().trim());
            long l = openHelper.insertData(studentBean);
            if (l>0){
                Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "写入失败", Toast.LENGTH_SHORT).show();
            }
            students.clear();
            students.addAll(openHelper.queryAllFromDb());
            studentAdapter.notifyDataSetChanged();
        });

        btnDel.setOnClickListener(v -> {
            int i = openHelper.deleteDataFromDbByName(etName.getText().toString().trim());
            if (i != -1){
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();
            }
        });

        btnUpdate.setOnClickListener(v -> {
            StudentBean student = new StudentBean();
            student.setName(etName.getText().toString().trim());
            student.setNumber("202409186789");
            student.setGender("female");
            student.setScore("100");
            int i = openHelper.updateDataByName(student);
            if (i>0){
                Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this, "更新失败", Toast.LENGTH_SHORT).show();
            }
        });

        btnQuery.setOnClickListener(v -> {

            List<StudentBean> studentBeans = openHelper.queryFromDbByName(etName.getText().toString().trim());
            String str_info = "";
            if (studentBeans.size() == 0){
                str_info = "没有查询到数据";
            }else {
                str_info = "查询到" + studentBeans.size() + "人" + "\n";
            }
            Log.d("MainActivity", etName.getText().toString().trim() + " studentBeans.size() is " + String.valueOf(studentBeans.size()));
            StringBuilder str_students = new StringBuilder();
            for (StudentBean student: studentBeans){
                str_students.append(student.getName()).append(",").append(student.getNumber())
                        .append(",").append(student.getGender()).append(",")
                        .append(student.getScore()).append("\n");
            }

            String str1 = str_info + str_students;

            tvStudents.setText(str1);
        });
    }

    private void initView() {
        etName = findViewById(R.id.et_name);
        etNumber = findViewById(R.id.et_number);
        etGender = findViewById(R.id.et_gender);
        etScore = findViewById(R.id.et_score);
        btnInsert = findViewById(R.id.btn_insert);
        btnDel = findViewById(R.id.btn_del);
        btnUpdate = findViewById(R.id.btn_update);
        btnQuery = findViewById(R.id.btn_query);
        tvStudents = findViewById(R.id.tv_students);
        lvStudents = findViewById(R.id.lv_students);
    }
}


